package com.tronsis.distribution.biz.impl;

import android.app.Activity;

import com.google.gson.reflect.TypeToken;
import com.tronsis.distribution.bean.CartBean;
import com.tronsis.distribution.bean.ProductBean;
import com.tronsis.distribution.bean.ProductCodeFindBean;
import com.tronsis.distribution.bean.ProductDetailBean;
import com.tronsis.distribution.biz.ProductBiz;
import com.tronsis.distribution.http.Url;
import com.tronsis.library.http.HttpCallBack;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.RequestClient;
import com.tronsis.library.http.RequestMethod;
import com.tronsis.library.http.UICallBack;

import java.util.List;

public class ProductBizImpl implements ProductBiz {
    private static final String TAG = "ProductBizImpl";


    @Override
    public void getProductList(Activity activity, int page, int size, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<ListBean<ProductBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("page", String.valueOf(page), "size",String.valueOf(size)).excute(RequestMethod.GET, Url.PRODUCT);

    }

    @Override
    public void getProductDetail(Activity activity, String productId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, ProductDetailBean.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.PRODUCT + "/" + productId);
    }

    @Override
    public void getCartList(Activity activity, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, new TypeToken<List<CartBean>>() {
        }.getType()) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.GET, Url.CART);
    }

    @Override
    public void addCart(Activity activity, String productId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter("productId", productId).excute(RequestMethod.POST, Url.CART);
    }

    @Override
    public void updateCartNum(Activity activity, String cartId, int amount, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter("amount", String.valueOf(amount)).excute(RequestMethod.POST, Url.CART + "/" + cartId);
    }

    @Override
    public void deleteCart(Activity activity, String cartId, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams().excute(RequestMethod.DELETE, Url.CART + "/" + cartId);

    }

    @Override
    public void submitCart(Activity activity, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, String.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addParameter().excute(RequestMethod.POST, Url.CART_SUBMIT);
    }

    @Override
    public void find(Activity activity, String code, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, ProductDetailBean.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("code",code).excute(RequestMethod.GET, Url.PRODUCT_FIND);
    }

    @Override
    public void findCodeDetail(Activity activity, String code, final UICallBack callBack) throws Exception {
        RequestClient.getInstance(new HttpCallBack(activity, ProductCodeFindBean.class) {
            @Override
            public void onStart() {
                callBack.onStart();
            }

            @Override
            public void onCallBack(int status, Object result) {
                switch (status) {
                    case 200:
                        callBack.onRefresh(result);
                        break;
                    default:
                        callBack.onFailure(status);
                        break;
                }
            }
        }).addGetParams("code",code).excute(RequestMethod.GET, Url.PRODUCT_FIND_CODE);
    }
}
